//
//  MBLoginViewModel.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBLoginViewModel.h"
#import "MBInputModel.h"
#import "MBLoginTableCellViewModel.h"
#import <UMPush/UMessage.h>

@implementation MBLoginViewModel

-(void)setUpData
{
    [super setUpData];
    
    MBInputModel* model1 = [[MBInputModel alloc]initWithTitle:@"login_name" andPlaceHolder:@"请输入账号" andKeyboardType:UIKeyboardTypeDefault andRightIcon:@"delete"];
    MBInputModel* model2 = [[MBInputModel alloc]initWithTitle:@"login_password" andPlaceHolder:@"请输入密码" andKeyboardType:UIKeyboardTypeDefault andRightIcon:@"kejian_nor"];
    NSArray* entityArray = @[model1,model2];
    
    [self factoryInitDataModelArray:entityArray cellViewModelClass:[MBLoginTableCellViewModel class]];
}

-(NSInteger)numberOfSections
{
    return 1;
}

-(NSInteger)numberOfRowInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)heightForHeaderInSection:(NSInteger)section
{
    return [UIView lf_sizeFromIphone6:78];
}

-(MBBaseTableCellViewModel *)cellViewModelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArray[indexPath.row];
}

-(void)clickLogin
{
    [MBUserNetTool userNetToolLoginWithParam:self.loginParam success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            MBLoginUserModel* userModel = [MBLoginUserModel yy_modelWithDictionary:response.result];
            [MBLoginUserModelArchieveTool userInfoAchieveToFile:userModel];
            
            [UMessage addAlias:[NSString stringWithFormat:@"%ld",(long)userModel.user_id] type:[NSString stringWithFormat:@"%ld",(long)userModel.user_id] response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
                
            }];

            self.loginSuccess = YES;
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"登录失败，请稍后重试！"];
    }];
}

-(MBLoginParam *)loginParam
{
    if (_loginParam == nil) {
        _loginParam = [[MBLoginParam alloc]init];
        _loginParam.type = 2;
    }
    return _loginParam;
}

@end
